#include "mesh_join.h"
//#include "billboard.h"
#include <lampe/xml/IXMLFactoryEnvironment.h>
#include "irrxml.h"
#include <lampe/xml/IXmlNode.h>
#include <ISceneNodeAnimator.h>
#include <ISceneManager.h>
#include <IAnimatedMeshSceneNode.h>

namespace lampe{
namespace xml{
class MeshJoinFactory:public IIrrXmlNode{
public:
	MeshJoinFactory(irr::IrrlichtDevice*d):IIrrXmlNode(d){}
	virtual LAMPE_VECTOR_VENDOR<String> getProcessedTags(){
		LAMPE_VECTOR_VENDOR<String> list;
		list.push_back("mesh:joint");
		return list;
	};

	virtual lampe::xml::TagResult tagHandlerBefor(lampe::xml::IXMLFactoryEnvironment*environment,lampe::xml::IXmlNode*node){
		

		if (!node->getParent() || !node->getParent()->getTag())
			return lampe::xml::STOP;

		String joinName=node->getAttString("name","");

		if (joinName=="")
			return lampe::xml::STOP;

		irr::scene::IAnimatedMeshSceneNode*parent=(irr::scene::IAnimatedMeshSceneNode*)node->getParent()->getTag();
		irr::scene::IBoneSceneNode*join=parent->getJointNode(joinName.c_str());
		if (!join)
			return lampe::xml::STOP;

		node->setTag(join);
		return lampe::xml::NEXT;
	}
};

}//namespace xml
}//namespace lampe

void regMeshJoin(lampe::xml::IXMLFactoryEnvironment*environment,irr::IrrlichtDevice*d){
	lampe::xml::IFactoryRecord*h=new lampe::xml::MeshJoinFactory(d);
	environment->registerHandler(h);
	h->drop();
}